package com.ptswitch.material.pages.contract;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hxzon.project.AppRuntimeException;
import org.hxzon.util.BigDecimalUtil;
import org.hxzon.util.DateFormatUtil;
import org.hxzon.util.EMailUtil;
import org.hxzon.util.EMailUtil.EMail;

import com.ptswitch.material.model.contract.Contract;
import com.ptswitch.material.model.contract.ContractInvoice;
import com.ptswitch.material.pages.base.MaterialBasePage;
import com.ptswitch.material.service.ContractInvoiceService;
import com.ptswitch.material.service.ContractService;
import com.ptswitch.material.util.MaterialUtil;

public class AskInvoice extends MaterialBasePage {
    @Inject
    private ContractService contractSer;
    @Inject
    private ContractInvoiceService contractInvoiceSer;
    @Property
    private Contract contract;
    @Property
    private ContractInvoice invoice;
    //
    @InjectPage
    private ViewContract viewContract;

    @OnEvent(value = EventConstants.ACTIVATE)
    Object init(String id) {
        // users = userSer.findAll();
        contract = contractSer.findById(id);
        if (contract.isContractOk()) {
            setMessage("合同已完成");
            return viewContract.initialize(contract.getId());
        }
        invoice = new ContractInvoice();
        invoice.setReciever(contract.getFinancial());
        return null;
    }

    @OnEvent(value = EventConstants.PASSIVATE)
    Object onPassivate() {
        return contract.getId();
    }

    @OnEvent(value = EventConstants.PREPARE)
    void prepareValidateRestrict() {
        invoice.setPaymentCurrent(BigDecimalUtil.subtract(contract.getTotalPrice(), contract.getInvoicePaid()));
        invoice.setPayment(invoice.getPaymentCurrent());
    }

    @OnEvent(value = EventConstants.SUCCESS, component = "askInvoice")
    Object addInvoice() {
        checkUserIsEqual(contract.getCreater());
        //check invoice
        if (BigDecimalUtil.biggerThan(invoice.getPayment(), BigDecimalUtil.subtract(contract.getTotalPrice(), contract.getInvoicePaid()))) {//FIXME
            throw AppRuntimeException.error("申请的发票金额大于合同所需");
        }
        //save
        invoice.setSender(getVisitor());
        invoice.setContract(contract);
        invoice.setInvoiceType(ContractInvoice.Askfor_Invoice);
        invoice.setCreateTime(DateFormatUtil.formatToUniteTime());
        contractInvoiceSer.save(invoice);
        //
        contract.updateForAddInvoice(invoice);
        contractSer.save(contract);
        // send email
        EMail email = getMaterialUtil().newEMail();
        email.addTo(invoice.getReciever().getEmail());
        email.setSubject(invoice.getSender().getRealname() + "向你申请开具发票");
        email.addHtml(MaterialUtil.linkForViewContract(getWebUrl(), contract));
        email.addText("申请发票金额：" + invoice.getPayment());
        email.addText("申请发票备注：" + invoice.getRemark());
        email.addHtml(MaterialUtil.linkForReplyInvoice(getWebUrl(), invoice));
        EMailUtil.send(email);
        return viewContract.initialize(contract.getId());
    }
}
